package com.leyantech.ecp.agenx.web.converter.input;

import static com.leyantech.ai.ServiceType.QUESTION;

import com.leyantech.ai.DialogDirection;
import com.leyantech.ai.DialogueQuestion;
import com.leyantech.ai.Extension;
import com.leyantech.ai.Extension.Builder;
import com.leyantech.ai.ServiceMode;
import com.leyantech.ecp.agenx.common.converter.input.DialogueQuestionConverter;
import com.leyantech.utility.UuidUtils;

import io.vertx.core.json.JsonObject;
import org.apache.commons.lang3.StringUtils;

import java.util.UUID;

/**
 * @author eahau, {@literal <eahau@foxmail.com>}
 * @date 2022-11-01.
 */
public class PinDuoDuoBlockingQuestionConverter implements DialogueQuestionConverter {

  @Override
  public DialogueQuestion apply(JsonObject jsonObject) {

    String text = jsonObject.getString("content");
    if (StringUtils.isBlank(text)) {
      return DialogueQuestion.getDefaultInstance();
    }

    DialogueQuestion.Builder builder = DialogueQuestion.newBuilder();

    // token 中获取sid
    String storeId = jsonObject.getString("sid");

    String userId = jsonObject.getString("user_nick");

    if (StringUtils.isBlank(userId)) {
      userId = "言准测试";
    }

    String assistantId = jsonObject.getString("assistant_nick");

    if (StringUtils.isBlank(assistantId)) {
      assistantId = storeId + ":言准测试";
    }

    String sessionId = String.join(".", storeId, userId);

    String messageId = UuidUtils.uuidToString(UUID.randomUUID());
    String uuid = UuidUtils.uuidToString(UUID.randomUUID());

    builder.setRetailStoreId(storeId)
        .setAssistantId(assistantId)
        .setUserId(userId)
        .setSessionId(sessionId)
        .setMessageId(messageId)
        .setTimestamp(System.currentTimeMillis())
        .setUuid(uuid)
        .setServiceType(QUESTION)
        .setServiceMode(ServiceMode.NORMAL)
        .setApp("chat-pdd")
        .setDirection(DialogDirection.INCOME)
        .setTestWinModeValue(jsonObject.getInteger("test_win_mode"))
        .setQuery(text);

    final String msgType = jsonObject.getString("msgType", "");
    String spuId = "";
    String tradeId = "";
    String tradeInfo = "";
    switch (msgType) {
      case "ITEM_LINK":
        spuId = jsonObject.getString("spu_id");
        if (StringUtils.isNotBlank(spuId)) {
          builder.setQuery("https://mobile.yangkeduo.com/goods.html?goods_id=" + spuId);
        }
        break;
      case "ORDER":
        if (StringUtils.isNoneBlank(tradeId = jsonObject.getString("trade_id"),
            tradeInfo = jsonObject.getString("tradeInfo"))) {
          builder.setQuery("订单编号:" + tradeId);
        }
        break;
    }

    Builder extensionBuild = Extension.newBuilder();
    if (StringUtils.isNotBlank(spuId)) {
      extensionBuild.setItemId(spuId);
    }

    if (StringUtils.isNotBlank(tradeId)) {
      extensionBuild.setOrderId(tradeId);
    }

    if (StringUtils.isNotBlank(tradeInfo)) {
      extensionBuild.setMockTradeJson(tradeInfo);
    }

    builder.setExtension(extensionBuild);

    return builder.build();
  }

}
